package com.gxgeek.javabasic.design;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gaoxing
 * @create 2018-07-13 下午2:42
 */
interface Visitor{
    public void visit(NodeA nodeA);
    public void visit(NodeB nodeB);
}

class VisitA implements Visitor{
    public void visit(NodeA nodeA){
        System.out.println("***visitA***");
        nodeA.action();
    }
    public void visit(NodeB nodeB){
        System.out.println("***visitA***");
        nodeB.action();
    }
}
class VisitB implements Visitor{
    public void visit(NodeA nodeA){
        System.out.println("***visitB***");
        nodeA.action();
    }
    public void visit(NodeB nodeB){
        System.out.println("***visitB***");
        nodeB.action();
    }
}
interface Node{
    public void accept(Visitor visitor);
}
class NodeA implements Node{
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
    public void action(){
        System.out.println("NodeA visited");
    }
}
class NodeB implements Node{
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
    public void action(){
        System.out.println("NodeB visited");
    }
}
class Context{
    List<Node> list = new ArrayList();
    public void add(Node node) {
        list.add(node);
    }
    public void visit(Visitor visitor) {
        for(Node node : list){
            node.accept(visitor);
        }
    }
}
public class Client {
    private static Context ctx = new Context();
    public static void main(String[] args) {
        ctx.add(new NodeA());
        ctx.add(new NodeB());
        ctx.visit(new VisitA());
        ctx.visit(new VisitB());
    }
}